package com.hoopchina.ui.search;

// import class
import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;

/**
 * SearchRecordEngine
 * 用于操作搜索历史数据库
 * 
 * @author Guo Baiyu
 */
public class SearchRecordEngine {
    // private static final int VERSION = 1;
    // private static final int MAX_COUNT = 30; // 最多记录了的条数 // TODO:备用
    private Context                     mContext  = null;
    private SearchHistoryDatabaseHelper mDBHelper = null;

    public static final long            MAX_COUNT = 30;

    /**
     * 
     * @param context
     */
    public SearchRecordEngine( Context context ) {
        mContext = context;
        mDBHelper = new SearchHistoryDatabaseHelper( mContext, "baidu_video_search_history_db" );
        try {
            mDBHelper.getReadableDatabase();
        }
        catch ( SQLiteException e ) {
            e.printStackTrace();
        }
        finally {
            if ( null != mDBHelper ) {
                mDBHelper.close();
            }
        }
    }

    /**
     * 获得搜索历史的数据
     */
    public List<SearchRecord> getSearchRecordList() {
        List<SearchRecord> list = new ArrayList<SearchRecord>();
        try {
            SQLiteDatabase db = mDBHelper.getWritableDatabase();
            Cursor cursor = db.rawQuery( "select * from search_history_table;", null );
            while ( cursor.moveToNext() ) {
                String record = cursor.getString( cursor.getColumnIndex( "search_record" ) );
                SearchRecord sr = new SearchRecord( record, 0 );
                list.add( sr );
            }
        }
        catch ( SQLException e ) {
            e.printStackTrace();
        }
        finally {
            if ( null != mDBHelper ) {
                mDBHelper.close();
            }
        }
        return list;
    }

    /**
     * 清除搜索历史
     */
    public void clearSearchRecord() {
        SQLiteDatabase db = mDBHelper.getWritableDatabase();
        try {
            db.execSQL( "DELETE FROM search_history_table WHERE id=0;" );
        }
        catch ( SQLException e ) {
            e.printStackTrace();
        }
        finally {
            if ( null != mDBHelper ) {
                mDBHelper.close();
            }
        }
    }

    /**
     * 添加记录
     */
    public void addSearchRecord( SearchRecord searchRecord ) {
        // 如果已经存在该记录
        if ( isExist( searchRecord ) ) {
            removeSearchRecord( searchRecord );
        }

        List<SearchRecord> list = null;

        long count = getSearchRecordCount();
        if ( MAX_COUNT <= count ) {
            // 删除最早一次保存的内容
            list = getSearchRecordList();
            removeSearchRecord( list.get( 0 ) );
        }

        // add the record to DB
        ContentValues value = new ContentValues();
        value.put( "id", searchRecord.getIndex() );
        value.put( "search_record", searchRecord.getSearchRecordText() );

        try {
            SQLiteDatabase db = mDBHelper.getWritableDatabase();
            db.insert( "search_history_table", null, value );
        }
        catch ( SQLException e ) {
            // mDBHelper.close();
            e.printStackTrace();
        }
        finally {
            if ( null != mDBHelper ) {
                mDBHelper.close();
            }
        }
    }

    /**
     * 删除记录，根据 SearchRecord
     */
    public void removeSearchRecord( SearchRecord searchRecord ) {
        String searchText = searchRecord.getSearchRecordText();
        try {
            SQLiteDatabase db = mDBHelper.getWritableDatabase();
            db.execSQL( "DELETE FROM search_history_table WHERE search_record=\'" + searchText
                    + "\';" );
        }
        catch ( SQLException e ) {
            e.printStackTrace();
        }
        finally {
            if ( null != mDBHelper ) {
                mDBHelper.close();
            }
        }
    }

    /**
     * 查询记录是否存在
     */
    boolean isExist( SearchRecord searchRecord ) {
        String searchText = searchRecord.getSearchRecordText();
        long count = 0;
        try {
            SQLiteDatabase db = mDBHelper.getWritableDatabase();
            Cursor cursor = db.rawQuery(
                    "select count(*) from search_history_table where search_record=\'" + searchText
                            + "\';", null );

            if ( cursor.moveToLast() ) {
                count = cursor.getLong( 0 );
            }
        }
        catch ( SQLException e ) {
            e.printStackTrace();
        }
        finally {
            if ( null != mDBHelper ) {
                mDBHelper.close();
            }
        }
        return ( count > 0 ) ? true : false;
    }

    /*
     * 根据index，删除记录
     */
    public void removeSearchRecord( int index ) {
        // mRecordList.remove(index);
    }

    /**
     * 获得记录数量
     */
    public long getSearchRecordCount() {
        try {
            SQLiteDatabase db = mDBHelper.getWritableDatabase();

            Cursor cursor = db.rawQuery( "select count(*) from search_history_table;", null );

            if ( cursor.moveToLast() ) {
                return cursor.getLong( 0 );
            }
        }
        catch ( SQLException e ) {
            e.printStackTrace();
        }
        finally {
            if ( null != mDBHelper ) {
                mDBHelper.close();
            }
        }

        return 0;
    }
}
